import ilog.concert.IloException;
import ilog.concert.IloNumVar;
import ilog.concert.IloRange;
import ilog.cplex.IloCplex;

public class homework1 {
        //solve the dual problem
    public void hh(){
        try {
        IloCplex cplex = new IloCplex(); // creat a model
        double[] lb = {0.0, 0.0, 0.0,0.0};
        double[] ub = {Double.MAX_VALUE,Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE};
        IloNumVar[] dualV = cplex.numVarArray(4, lb, ub);
        double[] dualobjvals = {48,20,8,5};
        cplex.addMinimize(cplex.scalProd(dualV, dualobjvals));
        double[] coeff1 = {8,4,2,0};
        double[] coeff2 = {6,2,1.5,1};
        double[] coeff3 = {1,1.5,0.5,0};
        cplex.addGe(cplex.scalProd(dualV, coeff1), 60);
        cplex.addGe(cplex.scalProd(dualV, coeff2), 30);
        cplex.addGe(cplex.scalProd(dualV, coeff3), 20);
        if (cplex.solve()) {
            cplex.output().println("Solution status = " + cplex.getStatus());
            cplex.output().println("Solution value = " + cplex.getObjValue());
            double[] val = cplex.getValues(dualV);
            for (int j = 0; j < val.length; j++)
                cplex.output().println("dual variable_" + (j+1) + " = " + val[j]);
        }
        cplex.end();
        } catch (
        IloException e) {
            System.err.println("Concert exception caught: " + e);
        }
    //get the reduced cost of the primal problem
        try {
        IloCplex cplex = new IloCplex(); // creat a model
        double[] lb = {0.0, 0.0, 0.0,0.0, 0.0, 0.0,0.0};
        double[] ub = {Double.MAX_VALUE,Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE,Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE};
        IloNumVar[] dualV = cplex.numVarArray(7, lb, ub);
        double[] dualobjvals = {60,30,20,0,0,0,0};
        cplex.addMaximize(cplex.scalProd(dualV, dualobjvals));
        IloRange[] con = new IloRange[4];
        double[] coeff1 = {8,6,1,1,0,0,0};
        double[] coeff2 = {4,2,1.5,0,1,0,0};
        double[] coeff3 = {2,1.5,0.5,0,0,1,0};
        double[] coeff4 = {0,1,0,0,0,0,1};
        con[0]=cplex.addLe(cplex.scalProd(dualV, coeff1), 48,"con0");
        con[1]=cplex.addLe(cplex.scalProd(dualV, coeff2), 20,"con1");
        con[2]=cplex.addLe(cplex.scalProd(dualV, coeff3), 8,"con2");
        con[3]=cplex.addLe(cplex.scalProd(dualV, coeff4), 5,"con3");
        if (cplex.solve()) {
            cplex.output().println("Solution status = " + cplex.getStatus());
            cplex.output().println("Solution value = " + cplex.getObjValue());
            double[] reducedval = cplex.getReducedCosts(dualV);
            for (int j = 0; j < 3; j++)
                cplex.output().println("primal variable reduced_x_" + (j+1) + " = " + reducedval[j]);
            for (int j = 3; j < reducedval.length; j++)
                cplex.output().println("slack variable reduced_s_" + (j-2) + " = " + reducedval[j]);
        }

        cplex.end();
        } catch (IloException e) {
            System.err.println("Concert exception caught: " + e);
        }
    }

}
